package com.xzc.algorithm.data_structure_ke.ArrayList.array;

import lombok.Data;

@Data
public class _0_Array {
    private int[] data;
    private int size;

    public _0_Array() {
        data = new int[10];
    }

    public _0_Array(int capability) {
        data = new int[capability];
    }

    public int getSize() {
        return size;
    }

    public int getCapability() {
        return data.length;
    }

    public int get(int index) {
        if (index < 0 || index > size - 1) {
            throw new IllegalArgumentException("索引必须大于等于0，小于等于size-1");
        }
        return data[index];
    }

    public void addFirst(int e) {
        add(0, e);
    }

    public void addLast(int e) {
        add(size, e);
    }

    public void add(int index, int e) {
        if (size == data.length)
            throw new IllegalArgumentException("数组已满");

        if (index < 0 || index > size) {
            throw new IllegalArgumentException("索引必须大于等于0，小于等于size");
        }

        for (int i = size - 1; i >= index; i--) {
            data[i + 1] = data[i];
        }
        data[index] = e;
        size++;
    }
}
